
package com.alibaba.otter.node.etl.autoconfigure;

import com.alibaba.otter.node.common.autoconfigure.NodeProperties;
import com.alibaba.otter.shared.common.utils.compile.impl.JdkCompiler;
import com.alibaba.otter.shared.common.utils.extension.DefaultExtensionFactory;
import com.alibaba.otter.shared.common.utils.extension.classpath.ClassScanner;
import com.alibaba.otter.shared.common.utils.extension.classpath.ClasspathClassScanner;
import com.alibaba.otter.shared.common.utils.extension.classpath.FileSystemClassScanner;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration(proxyBeanMethods = false)
public class NodeExtensionAutoConfiguration {


    private final NodeProperties nodeProperties;


    public NodeExtensionAutoConfiguration(NodeProperties nodeProperties) {
        this.nodeProperties = nodeProperties;
    }


    @Bean
    public JdkCompiler jdkCompiler() {
        return new JdkCompiler();
    }

    @Bean
    public ClasspathClassScanner classPathScanner() {
        return new ClasspathClassScanner();
    }


    @Bean
    public FileSystemClassScanner fileSystemScanner() {
        FileSystemClassScanner fileSystemClassScanner = new FileSystemClassScanner();
        fileSystemClassScanner.setExtendsDir(nodeProperties.getExtendDir());
        return fileSystemClassScanner;
    }


    @Bean
    public DefaultExtensionFactory extensionFactory(@Qualifier("classPathScanner") ClassScanner classPathScanner,
                                                    @Qualifier("fileSystemScanner") ClassScanner fileSystemScanner,
                                                    JdkCompiler jdkCompiler) {
        DefaultExtensionFactory defaultExtensionFactory = new DefaultExtensionFactory();
        defaultExtensionFactory.setClassPathScanner(classPathScanner);
        defaultExtensionFactory.setFileSystemScanner(fileSystemScanner);
        defaultExtensionFactory.setJdkCompiler(jdkCompiler);
        return defaultExtensionFactory;
    }
}
